<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/7
 * Time: 13:55
 */

namespace Portal\Controller;

use Common\Controller\AdminbaseController;
class CenterController extends AdminbaseController
{
    public function index(){
        $result=M('center')
            ->alias("a")
            ->field('a.*,b.name')
            ->join("__CENTER_CAT__ b ON a.c_id = b.id")
            ->select();
        $this->assign('result',$result);
        $this->display();
    }
    public function add(){
        $re=M('center_cat')->select();
        $this->assign('result',$re);
        $this->display();
    }
    public function add_post(){
        $data['title']=I('post.title');
        $data['c_id']=I('post.c_id');
        $data['content']=htmlspecialchars_decode(I('post.content'));
        $data['createtime']=date('Y-m-d H:i:s',time());
        if(M('center')->add($data)){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id=I('get.id');
        $result=M('center')
            ->alias("a")
            ->field('a.*,b.name')
            ->join("__CENTER_CAT__ b ON a.c_id = b.id")
            ->where("a.id=$id")
            ->find();
        $this->assign('result',$result);
        $cat=M('center_cat')->select();
        $this->assign('cat',$cat);
        $this->display();
    }
    public function edit_post(){
        $id=I('post.id');
        $data['title']=I('post.title');
        $data['c_id']=I('post.c_id');
        $data['content']=htmlspecialchars_decode(I('post.content'));
        if(M('center')->where("id=$id")->save($data)){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
        $this->display();
    }
    public function delete(){
        $id=I('get.id');
        if(M('center')->where("id = $id")->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    public function cat_index(){
        $re=M('center_cat')->select();
        $this->assign('result',$re);
        $this->display();
    }
    public function cat_add(){
        $this->display();
    }
    public function cat_add_post(){
        $data['name']=I('post.name');
        if(M('center_cat')->add($data)){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
        $this->display();
    }
    public function cat_edit(){
        $id=I('get.id');
        $re=M('center_cat')->where("id = $id")->find();
        $this->assign('result',$re);
        $this->display();
    }
    public function cat_edit_post(){
        $id=I('post.id');
        $data['name']=I('post.name');
        if(M('center_cat')->where("id=$id")->save($data)){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function cat_delete(){
        $id=I('get.id');
        if(M('center_cat')->where("id = $id")->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}